package edu.ucla.rendezvous;

import java.io.IOException;
import java.net.ServerSocket;

public class RendezvousServer {
	
	/** 
	 * Pass in the port number that you want to RendezvousServer to run on
	 * 
	 * @param args
	 * @throws IOException
	 */
	public static void main(String[] args) throws IOException {
		int portNumber = -1;
		boolean listneing = true;
		
		// You can pass in what port you want to start the RendezvousServer
		if(args.length < 1) {
			portNumber = 4444;
			System.out.println("Starting using default port: 4444");
		} else {
			try{ 
				portNumber = Integer.parseInt(args[0]);
				System.out.println("Starting using port: " + portNumber);
			} catch (NumberFormatException nfe) {
				System.out.println("Invalid format, starting using default port: 4444");
				nfe.printStackTrace();
				portNumber = 4444;
			}
		}
		
		
		ServerSocket serverSocket = null;
		try {
			serverSocket = new ServerSocket(portNumber);
		} catch (IOException e) {
			System.err.println("Could not listen on port: " + portNumber);
			System.exit(1);
		}
		
		while(listneing) { 
			new RendezvousServerThread(serverSocket.accept()).start(); 
		}
		serverSocket.close();
	}
}